package com.modern.js.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.modern.js.entity.Department;
import com.modern.js.entity.Role;
import com.modern.js.pojo.AssistPojo;
import com.modern.js.service.IRoleService;
import com.modern.js.utils.StringUtils;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author PIGS
 * @since 2020-03-23
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService iRoleService;

    @GetMapping("/list")
    public List<Role> selectAll(){
        return iRoleService.list();
    }

    /**
     *
     * @param page 当前页码
     * @param limit 每页的记录数
     * @return
     */
    @GetMapping("/listPage")
    public Map getList(Integer page, Integer limit,String deptName,String roleName ){
        Map<String,Object> map = new HashMap<String,Object>();
        Page<AssistPojo> rolePage = new Page<AssistPojo>();
        rolePage.setSize(limit);//设置每页记录数
        rolePage.setCurrent(page);//设置当前页码
        IPage<AssistPojo> roleIPage = iRoleService.selectAll(rolePage,deptName,roleName);
        if (roleIPage.getTotal()!=0){
            map.put("msg","查询成功！");
        }else {
            map.put("msg","查询失败！");
        }
        map.put("data",roleIPage.getRecords());
        map.put("count",roleIPage.getTotal());
        map.put("code",0);
        return map;
    }

    /**
     * 删除角色信息
     * @return
     */
    @DeleteMapping("/del")
    public boolean delRole(AssistPojo assistPojo){
        return iRoleService.delRole(assistPojo);

    }

    /**
     * 添加角色信息
     * @param role
     * @return
     */
    @PostMapping("/add")
    public boolean addRole(Role role,Integer deptId){
       boolean result = iRoleService.insertRole(role,deptId);
       return result;
    }

    /**
     * 修改角色信息
     * @param role
     * @return
     */
    @PutMapping("/update")
    public boolean updateRole(Role role,Integer deptId, Integer id){
        int result = iRoleService.updateRole(role,deptId,id);
        if (result==2){
            return true;
        }else {
            return false;
        }
    }

    @GetMapping("/selectRole")
    public List selectRoleDept(Integer deptId){
        List<AssistPojo> list = iRoleService.selectRoleDept(deptId);
        for (AssistPojo assistPojo:list){
            System.out.println("这是角色："+assistPojo);
        }
        return list;
    }
}
